﻿using System;
using System.Collections.Generic;
using CoreVirtuoso.Config;
using Infrastructure.GeoLabels.Interfaces;
using Infrastructure.Helpers;
using VDS.RDF;
using VirtuosoDal.Config;
using VirtuosoDal.Repositories.RdfRepositories.Interfaces.VirtuosoCore;

namespace Infrastructure.GeoLabels
{
   public class GeoLabelsAdditionService : IGeoLabelsAdditionService
   {
      private readonly ISequenceRepository _sequenceRepository;
      private readonly IGraphRepository _graphRepository;

      public GeoLabelsAdditionService(ISequenceRepository sequenceRepository, IGraphRepository graphRepository)
      {
         _sequenceRepository = sequenceRepository;
         _graphRepository = graphRepository;
      }

      public void AddGeoLabel(Dictionary<string, INode> dictionary, Graph graph)
      {
         int geoLabelId = _sequenceRepository.SequenceNext(Sequences.GeoLabelsSequenceSet);
         string geoLabelUri = Namespaces.GeoLabelsNamespace + geoLabelId;

         UriNode subject = graph.CreateUriNode(new Uri(geoLabelUri));

         dictionary[EventsOntologyUris.LastUsageDateTimePropertyUri] = NodeHelper.CreateDateTimeNode(graph, DateTime.UtcNow);

         foreach (string propertyUri in dictionary.Keys)
         {
            UriNode predicate = graph.CreateUriNode(new Uri(propertyUri));
            INode @object = dictionary[propertyUri];

            graph.Assert(subject, predicate, @object);
         }

         _graphRepository.AddTriplesToGraph(Namespaces.GeoLabelsNamespace, new List<Triple>(graph.Triples));
      }
   }
}
